//
//  PPAlertView.m
//  StatusBarEX
//
//  Created by Garenge on 2020/12/1.
//  Copyright © 2020 鹏鹏. All rights reserved.
//

#import "PPAlertView.h"

@interface PPAlertView()

@property (weak, nonatomic) IBOutlet UIButton *sureButton;

@end

@implementation PPAlertView

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.sureButton.layer.cornerRadius = 4;
    self.sureButton.layer.masksToBounds = YES;
    
    self.whiteButtonBGView.layer.cornerRadius = 4;
    self.whiteButtonBGView.layer.masksToBounds = YES;
}

+ (PPAlertView *)showAlertViewWithAlreadyExistWhiteButton:(UIView *)whiteButton timeOpeView:(UIView *)timeOpeView {
    
    /// 创建一个大view到window上
    PPAlertView *alertView = [PPAlertView createViewFromNib];
    
    UIWindow *window = UIApplication.sharedApplication.keyWindow;
    __block CGRect frame = window.bounds;
    frame.origin.y = - frame.size.height;
    alertView.frame = frame;
    [window addSubview:alertView];
    
    [UIView animateWithDuration:0.5 animations:^{
        frame.origin.y = 0;
        alertView.frame = frame;
    }];
    
    whiteButton.frame = alertView.whiteButtonBGView.bounds;
    [alertView.whiteButtonBGView addSubview:whiteButton];
    timeOpeView.frame = alertView.timeOpeViewBGView.bounds;
    [alertView.timeOpeViewBGView addSubview:timeOpeView];
    
    return alertView;
}

- (IBAction)sureButtonClickAction:(id)sender {
    /// 把这个view从界面上移除
    __block CGRect frame = self.frame;
    [UIView animateWithDuration:0.5 animations:^{
        frame.origin.y = - frame.size.height;
        self.frame = frame;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
        
        if (self.viewWillDismiss) {
            self.viewWillDismiss();
        }
    }];
}


+ (instancetype)createViewFromNib {
    return [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:self options:nil] lastObject];
}

@end
